package com.ygsoft.tianlu_mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygsoft.tianlu_mall.pojo.*;
import com.ygsoft.tianlu_mall.service.GoodsService;
import com.ygsoft.tianlu_mall.service.NumService;
import com.ygsoft.tianlu_mall.service.TraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    TraceService traceService;

    @Autowired
    NumService numService;

    //获得分类名称
    @RequestMapping(value = "/getAllGoodsType",method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllTypeName(){
        try {
            List<Map<String,Object>> typeList = goodsService.getToalTypeName();
            Map<Object,Object> result = new HashMap<>();
            result.put("typeList",typeList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //根据类型id获取参数名称
    @RequestMapping(value = "/getAllPropertyByTypeId",method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllPropertyByTypeId(@RequestParam int type_id){
        try {
            List<Map<String,Object>> propertyList = goodsService.getAllPropertyByTypeId(type_id);
            Map<Object,Object> result = new HashMap<>();
            result.put("propertyList",propertyList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //单个商品详情展示
    //isCollectCode 0:未收藏 1:已收藏
    @RequestMapping(value = "/getGoodsDetail",method = RequestMethod.GET)
    @ResponseBody
    public Msg getGoodsDetail(@RequestParam Integer user_id,@RequestParam("goods_id") Integer goods_id){
        try {
            List isCollectList = new ArrayList();
            int isCollectCode = 0;
            isCollectList.add(isCollectCode);
            Boolean isUserId = (user_id!=null);
            if (isUserId){
                //加入足迹
                traceService.addGoods2Trace(user_id,goods_id);
                //检查收藏状态
                HashMap<String,Object> isCollect = goodsService.checkIsCollect(user_id,goods_id);
                if (isCollect==null){
                    isCollectList.clear();
                    isCollectList.add(isCollectCode);
                }else {
                    isCollectCode = 1;
                    isCollectList.clear();
                    isCollectList.add(isCollectCode);
                }
            }
            List<Map<String,Object>> getGoodsInfo = goodsService.getGoodsDetailByGoodsId(goods_id);
            List<Map<String,Object>> propertyList = goodsService.getPropertyValueById(goods_id);
            Map<Object,Object> result = new HashMap<>();
            result.put("goodsInfo",getGoodsInfo);
            result.put("propertyList",propertyList);
            result.put("isCollect",isCollectList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //首页商品展示
    @RequestMapping(value = "/getIndexGoods",method = RequestMethod.GET)
    @ResponseBody
    public Msg getGoodsIndex(){

        try {
            Map<Object,Object> result = new HashMap<>();
            List<HashMap<String,Object>> goodsNum = new ArrayList<>();
            //获取商品信息
            List<Map<String,Object>> goodsInfo = goodsService.getGoodsIndex();
            List goods_idList = goodsService.getGoodsId();
            for (Object goods_id:goods_idList){
                HashMap<String,Object> goodsNumMap =numService.getThreeNum((int)goods_id);
                goodsNum.add(goodsNumMap);
            }
            result.put("goodsInfo",goodsInfo);
            result.put("goodsNum",goodsNum);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //分类商品展示
    //pageNum:开始页数 默认为1 pageSize：一页展示多少个
    @RequestMapping(value = "/getTypeGoods",method = RequestMethod.GET)
    @ResponseBody
    public Msg getTypeGoods(@RequestParam("type_id") int type_id,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam Integer pageSize){
        try {
            List<HashMap<String,Object>> goodsNum = new ArrayList<>();
            Map<Object,Object> result = new HashMap<>();
            PageHelper.startPage(pageNum,pageSize);
            List<Map<String,Object>> goodsByType = goodsService.getGoodsByTypeId(type_id);
            List goods_idList = goodsService.getGoodsIdByTypeId(type_id);
            for (Object goods_id:goods_idList){
                HashMap<String,Object> goodsNumMap =numService.getThreeNum((int)goods_id);
                goodsNum.add(goodsNumMap);
            }
            PageInfo goodsInfo = new PageInfo(goodsByType,5);
            result.put("goodsInfo",goodsInfo);
            result.put("goodsNum",goodsNum);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //推荐商品展示
    @RequestMapping(value = "/getRecommendGoodsByType",method = RequestMethod.GET)
    @ResponseBody
    public Msg getRecommendGoodsByType(@RequestParam int type_id){
        try {
            //按销量选出推荐商品
            Integer goods_id = (Integer) goodsService.getRecommendGoodsId(type_id).get("goods_id");
            if (!goods_id.equals(null)){
                List<Map<String,Object>> goodsInfo = goodsService.getRecommendGoodsByType(goods_id);
                List<HashMap<String,Object>> goodsNum = new ArrayList<>();
                goodsNum.add(numService.getThreeNum((int)goods_id));
                Map<Object,Object> result = new HashMap<>();
                result.put("goodsInfo",goodsInfo);
                result.put("goodsNum",goodsNum);
                return Msg.succes().add("result",result);
            }else {
                //指定分类下并未有订单
                //指定分类上架的第一件商品为推荐商品
                int goods_id2 = goodsService.getRecommendGoodsId2(type_id);
                List<Map<String,Object>> goodsInfo = goodsService.getRecommendGoodsByType(goods_id2);
                List<HashMap<String,Object>> goodsNum = new ArrayList<>();
                goodsNum.add(numService.getThreeNum((int)goods_id2));
                Map<Object,Object> result = new HashMap<>();
                result.put("goodsInfo",goodsInfo);
                result.put("goodsNum",goodsNum);
                return Msg.succes().add("result",result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }


    }

}
